package com.yy.permissions_general.module.system.menu.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.permissions_general.common.config.exception.base.ServiceException;
import com.yy.permissions_general.common.constant.SystemConstant;
import com.yy.permissions_general.module.system.menu.entity.Auth;
import com.yy.permissions_general.module.system.menu.mapper.AuthMapper;
import com.yy.permissions_general.module.system.menu.service.AuthService;
import com.yy.permissions_general.module.system.menu.vo.MenuVo;
import com.yy.permissions_general.module.system.roleAuth.entity.RoleAuth;
import com.yy.permissions_general.module.system.roleAuth.service.RoleAuthService;
import com.yy.permissions_general.module.system.user.entity.User;
import com.yy.permissions_general.utils.FastJsonUtils;
import com.yy.permissions_general.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author YY
 * @since 2021-08-07
 */
@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, Auth> implements AuthService {
    @Resource
    private RoleAuthService roleAuthService;

    @Resource
    private AuthMapper authMapper;
    /**
    * 分页查询列表
    * @param auth
    * @return
    */
    @Override
    public  Page<Auth>selectPageList(Auth auth, int limit, int page)  {
        Page pageContext = new Page(page,limit);
        Page<Auth> pageList = this.baseMapper.selectPage(pageContext, auth);
        return pageList;
    }

    /**
    * 新增数据
    * @param auth
    */
    @Override
    public int addAuth(Auth auth) {
        if (StringUtils.isNotBlank(auth.getAuthCode())){
            int resultInt = checkAuth(auth,false);
            if (resultInt == 1){
                throw new ServiceException(400,"权限标识重复~");
            }
        }
        return baseMapper.insert(auth);
    }

    /**
    * 修改数据
    * @param auth
    */
    @Override
    public int editAuth(Auth auth) {
        if (StringUtils.isNotBlank(auth.getAuthCode())){
            int resultInt = checkAuth(auth,true);
            if (resultInt == 1){
                throw new ServiceException(400,"权限标识重复~");
            }
        }
        return baseMapper.updateById(auth);
    }

    /**
     * 判断权限标识是否重复
     * @param auth
     * @param isUpdate
     * @return
     */
    private int checkAuth(Auth auth, boolean isUpdate) {
        int resultInt = 0;
        QueryWrapper<Auth> queryWrapper = new QueryWrapper<Auth>();
        if (isUpdate){
            queryWrapper.ne("id",auth.getId());
        }
        queryWrapper.eq("auth_code",auth.getAuthCode());
        Auth checkAuth = this.getOne(queryWrapper);
        if (checkAuth != null){
            resultInt = 1;
        }
        return resultInt;
    }
    /**
    * 查看详情
    * @param id
    * @return
    */
    @Override
    public Auth getDetailsById(Integer id) {
        return baseMapper.selectById(id);
    }

    /**
    * 删除数据
    * @param id
    * @return
    */
    @Override
    public int deleteById(Integer id) {
        /*判断是否存在子级*/
        QueryWrapper<Auth> queryWrapper = new QueryWrapper<Auth>();
        queryWrapper.eq("parent_id",id);
        List<Auth> list = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)){
            throw new ServiceException(400,"请先删除子节点~");
        }
        /*判断角色是否绑定菜单*/
        QueryWrapper<RoleAuth> roleAuthQueryWrapper = new QueryWrapper<RoleAuth>();
        roleAuthQueryWrapper.eq("auth_id",id);
        List<RoleAuth> roleAuths = roleAuthService.list(roleAuthQueryWrapper);
        if (CollectionUtil.isNotEmpty(roleAuths)){
            throw new ServiceException(400,"操作有误：已绑定角色~");
        }
        return baseMapper.deleteById(id);
    }

    /**
     * 首页初始化菜单
     * @return
     */
    @Override
    public Map<String,Object> selectAuthByUser() {
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("homeInfo",FastJsonUtils.toBean(SystemConstant.HOME_INFO));
        resultMap.put("logoInfo",FastJsonUtils.toBean(SystemConstant.LOGO_INFO));
        User user = SecurityUtil.getCurrentUser();
        user.setType("0,1");
        List<MenuVo> menuVoList= authMapper.selectMenuVoByUser(user);
        resultMap.put("menuInfo",MenuVo.menuVoUtil(0,menuVoList));
        return resultMap;
    }

}

